#!/bin/bash

file=/tmp/lcd_tick

if [ ! -r $file ]; then
  echo "Cant read $file"
  exit 1
fi

function restart_lcd {
  sudo killall -q lcd.py
  sudo /home/pi/siweather/scripts/lcd.py > /dev/null 2>&1 &
}

tick=`cat /tmp/lcd_tick`
echo "Tick: $tick"

if [ -z "$tick" ]; then
  echo "File $file is empty"
  restart_lcd
  exit 0
fi

sleep 4

tock=`cat /tmp/lcd_tick`
echo "Tock: $tock"

diff=`echo "$tock - $tick" | bc`
echo "Diff: $diff"

if [ $diff -lt 2 ]; then
  echo "No change in $file after 4 seconds, restarting lcd.py"
  restart_lcd
  exit 0
fi

